package com.mall.struts.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mall.ibatis.bean.Frame;
import com.mall.ibatis.bean.ReturnMessage;
import com.mall.service.FrameexService;
import com.mall.service.FunctionService;
import com.thoughtworks.xstream.XStream;

public class FrameexAction extends Action{
	private static final FunctionService functionService = FunctionService.getInstance();
	private static final FrameexService frameexService = FrameexService.getInstance();
	
	@Override
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws Exception{
		String type = request.getParameter("type");
		
		if("loading".equals(type)){
			return loading(mapping,form,request,response);
			
		}else if("frameexTreeLoading".equals(type)){
			return frameexTreeLoading(mapping,form,request,response);
			
		}else if("selectFrameex".equals(type)){
			return selectFrameex(mapping,form,request,response);
			
		}else if("saveFrameex".equals(type)){
			return saveFrameex(mapping,form,request,response);
			
		}else if("deleteFrameex".equals(type)){
			return deleteFrameex(mapping,form,request,response);
			
		}else if("addSubFrameex".equals(type)){
			return addSubFrameex(mapping,form,request,response);
			
		}
		return null;
	}
	
	//新增子节点
	private ActionForward addSubFrameex(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String subFrameexParentID = request.getParameter("subFrameexParentID");
		String subFrameexName = request.getParameter("subFrameexName");
		String subFrameexClass = request.getParameter("subFrameexClass");
		String subFrameexStatus = request.getParameter("subFrameexStatus");
		String subFrameexFlag = request.getParameter("subFrameexFlag");
		String subFrameexMemo = request.getParameter("subFrameexMemo");
		
		List<String> frameNameList = null;
		if(subFrameexParentID!=null && !subFrameexParentID.trim().equals("")){
			frameNameList = frameexService.selectFrameexNameByPId(subFrameexParentID);
		}
		
		if(frameNameList!=null){
			for(String s : frameNameList){
				if(s.equals(subFrameexName.replaceAll("\\s", ""))){
					ReturnMessage returnMessage = new ReturnMessage("3","同级节点下存在'"+s+"'");
					response.setContentType("text/xml");
					PrintWriter out = response.getWriter();
					out.print(returnMessage.toXMLString());
					out.flush();
					out.close();
					return null;
				}
			}
		}
		
		Frame frameex = new Frame();
		//frame.setFrameID(frameID);
		frameex.setFrameName(subFrameexName);
		frameex.setFrameParentID(subFrameexParentID);
		frameex.setFrameClass(subFrameexClass);
		frameex.setFrameFlag(subFrameexFlag);
		frameex.setFrameStatus(subFrameexStatus);
		frameex.setFrameMemo(subFrameexMemo);
		
		ReturnMessage returnMessage = frameexService.addSubFrameex(frameex);
		
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		out.print(returnMessage.toXMLString());
		out.flush();
		out.close();
		return null;
	}

	//节点删除
	private ActionForward deleteFrameex(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String frameexID = request.getParameter("frameexID");
		
		Frame frameex = new Frame();
		frameex.setFrameID(frameexID);
		
		ReturnMessage returnMessage = frameexService.deleteFrameex(frameex);
		
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		out.print(returnMessage.toXMLString());
		out.flush();
		out.close();
		return null;
	}

	//节点修改保存
	private ActionForward saveFrameex(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String frameexID = request.getParameter("frameexID");
		String frameexName = request.getParameter("frameexName");
		String frameexParentID = request.getParameter("frameexParentID");
		String frameexClass = request.getParameter("frameexClass");
		String frameexFlag = request.getParameter("frameexFlag");
		String frameexStatus = request.getParameter("frameexStatus");
		String frameexMemo = request.getParameter("frameexMemo");
		
		Frame frameex = new Frame();
		frameex.setFrameID(frameexID);
		frameex.setFrameName(frameexName);
		frameex.setFrameParentID(frameexParentID);
		frameex.setFrameClass(frameexClass);
		frameex.setFrameFlag(frameexFlag);
		frameex.setFrameStatus(frameexStatus);
		frameex.setFrameMemo(frameexMemo);
		
		ReturnMessage returnMessage = frameexService.saveFrameex(frameex);
		
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		out.print(returnMessage.toXMLString());
		out.flush();
		out.close();
		return null;
	}

	//查找节点数据
	private ActionForward selectFrameex(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String frameexID = request.getParameter("frameexID");
		
		Frame frame = frameexService.selectFrameex(frameexID);
		
		XStream xStream = new XStream();
		xStream.alias("frame",Frame.class);
		String xmlString = xStream.toXML(frame);
		
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		out.print(xmlString);
		out.flush();
		out.close();
		return null;
	}

	//管理架构树的加载
	private ActionForward frameexTreeLoading(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,HttpServletResponse response) {
		//管理架构树传过来的父节点id
		String dir = request.getParameter("dir");
		List<Frame> list = frameexService.frameexTreeLoading(dir);
		//采用和frame营销管理架构同一个jsp文件（树文件）
		request.setAttribute("frameList",list);
		return mapping.findForward("tree");
	}

	//页面的加载
	private ActionForward loading(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		functionService.methodFilter(request, response);
		return mapping.findForward("loading");
	}

}
